#include <iostream>
#include <vector>
#include <queue>

using namespace std;

class Solution {
public:
    int m, n;
    int dx[4] = { 0 , 0 , 1 , -1 };
    int dy[4] = { 1 , -1 , 0 , 0 };
    bool vis[20][20] = { false };

    int orangesRotting(vector<vector<int>>& grid) {
        m = grid.size(), n = grid[0].size();
        queue<pair<int, int>> q;
        int ret = -1, fresh = 0, bad = 0;

        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (grid[i][j] == 1)
                    ++fresh;
                else if (grid[i][j] == 2)
                {
                    q.push({ i , j });
                    ++bad;
                }
            }
        }

        while (!q.empty())
        {
            ++ret;
            int sz = q.size();
            for (int i = 0; i < sz; ++i)
            {
                auto p = q.front(); q.pop();

                for (int k = 0; k < 4; ++k)
                {
                    int a = dx[k] + p.first, b = dy[k] + p.second;
                    if (a >= 0 && a < m && b >= 0 && b < n && grid[a][b] == 1)
                    {
                        grid[a][b] = 2;
                        q.push({ a , b });
                        --fresh;
                    }
                }
            }
        }

        if (bad == 0 && fresh == 0)
            return 0;
        if (fresh)
            return -1;
        return ret;
    }
};
int main()
{

	return 0;
}